package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.*;
import com.zhentao.pojo.*;
import com.zhentao.service.PatientService;
import com.zhentao.vo.QueryParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* @author ls
* @description 针对表【patient】的数据库操作Service实现
* @createDate 2025-05-07 21:25:14
*/
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService{
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private PatientDepartmentMapper patientDepartmentMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private DoctorPatientMapper doctorPatientMapper;
    @Autowired
    private DoctorMapper doctorMapper;

    @Override
    public Page<Patient> findPage(QueryParam queryParam) {


        Page<Patient> page = new Page<>(queryParam.getCurrent(),queryParam.getSize());
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(queryParam.getPname()!=null,"pname",queryParam.getPname());
        patientMapper.selectPage(page,queryWrapper);
        return page;

        }



    @Override
    public List<Department> findDepartment(Integer patientId) {
        QueryWrapper<PatientDepartment> patientDepartmentQueryWrapper = new QueryWrapper<>();
        patientDepartmentQueryWrapper.eq("pid",patientId);
        List<PatientDepartment> patientDepartments = patientDepartmentMapper.selectList(patientDepartmentQueryWrapper);
        List<Department> departmentName=new ArrayList<>();
        for (PatientDepartment patientDepartment : patientDepartments) {
            Integer did = patientDepartment.getDid();
            Department department = departmentMapper.selectById(did);
            departmentName.add(department);
        }
        return departmentName;
    }

    @Override
    public List<Doctor> findDoc(Integer patientId) {
        QueryWrapper<DoctorPatient> doctorPatientQueryWrapper = new QueryWrapper<>();
        doctorPatientQueryWrapper.eq("pid", patientId);
        System.err.println(patientId);
        List<DoctorPatient> doctorPatients = doctorPatientMapper.selectList(doctorPatientQueryWrapper);

        List<Doctor> doctorList = new ArrayList<>();

        for (DoctorPatient doctorPatient : doctorPatients) {
            Integer did = doctorPatient.getDid();
            Doctor doctor = doctorMapper.selectById(did);
                doctorList.add(doctor);
        }

        // 如果无关联医生，返回空列表而非null
        return doctorList;
    }

    @Override
    public Patient findPatientById(Integer patientId) {
        Patient patient = patientMapper.selectById(patientId);
        return patient;
    }
}




